/*
 * Created by JFormDesigner on Wed May 07 17:06:03 CST 2025
 */

package com.zjj.bams.client;

import com.zjj.bams.entity.*;
import com.zjj.bams.exception.*;
import com.zjj.bams.entity.*;
import com.zjj.bams.exception.BalanceNotEnoughException;
import com.zjj.bams.exception.MoneyException;
import com.zjj.bams.exception.NotFoundException;
import com.zjj.bams.exception.TypeException;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.SocketException;
import javax.swing.*;
/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */

public class BusinessPanel extends JPanel {
    private ATMClient atm;

    public BusinessPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    //初始化业务界面
    public void initBusiness(){
        //使用 atm.acc 共有账户资源 进行初始化数据
        IDText.setText(atm.acc.getId()+"");
        nameText.setText(atm.acc.getName());
        balanceText.setText(atm.acc.getBalance()+"");
        moneyTextField.setText(0.0+"");

        //透支额度
        if(atm.acc instanceof CreditAccount){
            ceilingText.setText(((CreditAccount) atm.acc).getCeiling() +"");
        }else{
            ceilingText.setText("该账户不支持透支操作");
        }

        //贷款额度
        if(atm.acc instanceof LoanSavingAccount){
            loanText.setText(((LoanSavingAccount) atm.acc).getLoanAmount() +"");
        }else if(atm.acc instanceof LoanCreditAccount){
            loanText.setText(((LoanCreditAccount) atm.acc).getLoanAmount() +"");
        }else{
            loanText.setText("该账户不支持贷款操作");
        }

        //下拉菜单  不同账户类型，展示不同的可操作项
        if(atm.acc instanceof LoanSavingAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","贷款","还贷"}));
        }else if(atm.acc instanceof LoanCreditAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度","贷款","还贷"}));
        }else if(atm.acc instanceof CreditAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账","修改透支额度"}));
        }else if(atm.acc instanceof SavingAccount){
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"存款","取款","转账"}));
        }
    }

    //提交业务操作  执行Bank业务 -> 刷新页面
    private void submit(ActionEvent e) {
        Long id = Long.valueOf(IDText.getText());
        String moneyText = moneyTextField.getText();
        String select = comboBox.getSelectedItem().toString();

        if(moneyText==null || moneyText.equals("")){
            JOptionPane.showMessageDialog(null , "操作金额不能为空");
            return;
        }
        double money = Double.parseDouble(moneyText);
        if(money<=0.0){
            JOptionPane.showMessageDialog(null , "操作金额不能<=0.0");
            return;
        }

        Account acc = null;
        switch (select){
            case "存款":
                try {
                    acc = atm.bank.deposit(id , money);
                } catch (NotFoundException | MoneyException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;
            case "取款":
                String pwd = JOptionPane.showInputDialog(null , "请输入您的密码：");
                try {
                    acc = atm.bank.withdraw(id , pwd , money);
                } catch (NotFoundException | MoneyException | BalanceNotEnoughException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;
            case "转账":
                String toIdStr = JOptionPane.showInputDialog(null , "请输入转入账户ID信息：");
                String password = JOptionPane.showInputDialog(null , "请输入您的密码：");
                if(toIdStr.matches("\\d+")){
                    //未添加网络编程
                    /*Long to = Long.valueOf(toIdStr);
                    try {
                        boolean flag = atm.bank.transfer(id , password ,to , money);
                        if(flag){
                            acc = atm.bank.login(id,password);
                        }
                    } catch (NotFoundException | MoneyException | BalanceNotEnoughException | TransferException |
                             LoginException ex) {
                        JOptionPane.showMessageDialog(null , ex.getMessage());
                        moneyTextField.setText(0.0+"");
                    }*/

                    //添加网络编程
                    //1.写指令
                    atm.pw.println("transfer#"+id+"#"+password+"#"+toIdStr+"#"+money);
                    atm.pw.flush();

                    //2.读结果
                    try {
                        Object o = atm.ois.readObject();
                        if(o instanceof Account){
                            acc = (Account) o;
                        }else if(o instanceof String){
                            JOptionPane.showMessageDialog(this , o);
                            moneyTextField.setText(0.0+"");
                        }
                    } catch (SocketException ex){
                        JOptionPane.showMessageDialog(this , "服务器已关闭");
                        System.exit(0);
                    } catch (IOException | ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }

                }

                break;
            case "修改透支额度":
                pwd = JOptionPane.showInputDialog(null , "请输入您的密码：");
                try {
                    acc = atm.bank.updateCeiling(id , pwd , money);
                } catch (NotFoundException | MoneyException | TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;
            case "贷款":
                try {
                    acc = atm.bank.requestLoan(id , money);
                } catch (NotFoundException | MoneyException | TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;
            case "还贷":
                try {
                    acc = atm.bank.payLoan(id , money);
                } catch (NotFoundException | MoneyException | TypeException ex) {
                    JOptionPane.showMessageDialog(null , ex.getMessage());
                    moneyTextField.setText(0.0+"");
                }
                break;
        }

        //刷新界面
        if(acc!=null){
            atm.acc = acc;
            //初始化业务界面
            initBusiness();
        }
    }

    //跳转至主界面，并清空当前业务界面数据信息
    private void back(ActionEvent e) {
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        cardLayout.show(this.getParent() , "mainPanel");

        //清空当前业务界面数据信息
        clear();
    }

    //清空业务界面
    private void clear() {
        IDText.setText("");
        nameText.setText("");
        balanceText.setText("");
        ceilingText.setText("");
        loanText.setText("");
        comboBox.setSelectedIndex(0);
        moneyTextField.setText(0.0+"");
    }

    //定时刷新 ->  初始化界面initBusiness()

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        nameLabel = new JLabel();
        titleLabel = new JLabel();
        IDLabel = new JLabel();
        balanceLabel = new JLabel();
        ceilingLabel = new JLabel();
        loanLabel = new JLabel();
        IDText = new JLabel();
        nameText = new JLabel();
        balanceText = new JLabel();
        ceilingText = new JLabel();
        loanText = new JLabel();
        comboBox = new JComboBox<>();
        moneyTextField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- nameLabel ----
        nameLabel.setText("\u7528  \u6237  \u540d");
        nameLabel.setHorizontalAlignment(SwingConstants.LEFT);
        add(nameLabel);
        nameLabel.setBounds(120, 180, 90, 30);

        //---- titleLabel ----
        titleLabel.setText("\u4e1a  \u52a1  \u754c  \u9762");
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        titleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 20));
        add(titleLabel);
        titleLabel.setBounds(160, 45, 170, 45);

        //---- IDLabel ----
        IDLabel.setText("\u8d26  \u6237  ID");
        IDLabel.setHorizontalAlignment(SwingConstants.LEFT);
        add(IDLabel);
        IDLabel.setBounds(120, 130, 90, 30);

        //---- balanceLabel ----
        balanceLabel.setText("\u4f59         \u989d");
        balanceLabel.setHorizontalAlignment(SwingConstants.LEFT);
        add(balanceLabel);
        balanceLabel.setBounds(120, 230, 90, 30);

        //---- ceilingLabel ----
        ceilingLabel.setText("\u4fe1 \u7528 \u989d \u5ea6");
        ceilingLabel.setHorizontalAlignment(SwingConstants.LEFT);
        add(ceilingLabel);
        ceilingLabel.setBounds(120, 280, 90, 30);

        //---- loanLabel ----
        loanLabel.setText("\u8d37 \u6b3e \u989d \u5ea6");
        loanLabel.setHorizontalAlignment(SwingConstants.LEFT);
        add(loanLabel);
        loanLabel.setBounds(120, 330, 90, 30);

        //---- IDText ----
        IDText.setHorizontalAlignment(SwingConstants.CENTER);
        add(IDText);
        IDText.setBounds(225, 130, 160, 30);

        //---- nameText ----
        nameText.setHorizontalAlignment(SwingConstants.CENTER);
        add(nameText);
        nameText.setBounds(225, 180, 160, 30);

        //---- balanceText ----
        balanceText.setHorizontalAlignment(SwingConstants.CENTER);
        add(balanceText);
        balanceText.setBounds(225, 230, 160, 30);

        //---- ceilingText ----
        ceilingText.setHorizontalAlignment(SwingConstants.CENTER);
        add(ceilingText);
        ceilingText.setBounds(225, 280, 160, 30);

        //---- loanText ----
        loanText.setHorizontalAlignment(SwingConstants.CENTER);
        add(loanText);
        loanText.setBounds(225, 330, 160, 30);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u5b58\u6b3e",
            "\u53d6\u6b3e",
            "\u8f6c\u8d26",
            "\u4fee\u6539\u900f\u652f\u989d\u5ea6",
            "\u8d37\u6b3e",
            "\u8fd8\u8d37"
        }));
        add(comboBox);
        comboBox.setBounds(120, 400, 125, 35);

        //---- moneyTextField ----
        moneyTextField.setText("0.0");
        add(moneyTextField);
        moneyTextField.setBounds(270, 400, 115, 35);

        //---- submitButton ----
        submitButton.setText("\u63d0\u4ea4");
        submitButton.addActionListener(e -> submit(e));
        add(submitButton);
        submitButton.setBounds(135, 465, 100, 35);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(260, 465, 100, 35);

        setPreferredSize(new Dimension(480, 600));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel nameLabel;
    private JLabel titleLabel;
    private JLabel IDLabel;
    private JLabel balanceLabel;
    private JLabel ceilingLabel;
    private JLabel loanLabel;
    private JLabel IDText;
    private JLabel nameText;
    private JLabel balanceText;
    private JLabel ceilingText;
    private JLabel loanText;
    private JComboBox<String> comboBox;
    private JTextField moneyTextField;
    private JButton submitButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
